package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.mapper.SetmealDishMapper;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional//0.涉及多张表的增删改操作，使用事务
    public void saveWithFlavor(DishDTO dto) {
        //目标：菜品数据，口味数据存入数据库
        //1.构造菜品数据，菜品信息插到dish表中
        Dish dish = new Dish();
        //拷贝属性值
        BeanUtils.copyProperties(dto, dish);
        dishMapper.insert(dish);
        //获取dish的主键值
        Long dishId = dish.getId();
        log.info("菜品id：{}", dishId);
        //2.构造口味数据，口味信息插到dish_flavor表中
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishId);
            });
            log.info(" flavors:{}", flavors);
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        //1.分页插件,设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //2.调用mapper的列表查询方法
        Page<DishVO> dishPage = dishMapper.pageQuery(dto);
        //3.封装并返回结果对象
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id -> {
            //1.判断菜品是否启售，如果启售，禁止删除。
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //2.判断菜品是否被套餐关联，如果被关联，禁止删除。
            //根据菜品id查询套餐菜品关系表--统计查询
            Integer count = setmealDishMapper.count(id);
            if (count > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        });
        for (Long id : ids) {
            //3.删除菜品基本信息。
            dishMapper.deleteById(id);
            //4.删除菜品的口味列表数据。(根据菜品id删除)
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO getById(Long id) {
        DishVO dishVO = new DishVO();

        //1.调用dishMapper,根据id查询菜品基本信息，封装到DishVO中
        Dish dish = dishMapper.getById(id);
        BeanUtils.copyProperties(dish, dishVO);

        //2.调用dishFlavorMapper,根据id查询口味列表数据，封装到DishVO中
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(flavors);

        //3.返回DishVO对象
        return dishVO;
    }
}
